//
//  ReimbursementVC.m
//  BrushFace
//
//  Created by timer_q on 16/1/22.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "ReimbursementVC.h"
#import "ARRequestModel.h"
#import "RePayConpousVC.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "WeChat.h"
#import "AlertDialogHelper.h"
@interface ReimbursementVC ()<RePayConpousVCDelegate>
{
    NSDictionary *_currentDict;
    BOOL _isUseConpous;
    NSString *_conpousID;
    NSString *_conpousType;
    JSValue *_currentRuleJS;
    NSString *_couponsNum;
}
@property (weak, nonatomic) IBOutlet UILabel *priodLabel;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *littleMoneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *shengYuMoneyLabel;

@property (weak, nonatomic) IBOutlet UILabel *conpousLabel;
@end

@implementation ReimbursementVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    [self.navBar setTitle:@"还款"];
    [self.navBar setNavBackColor:[UIColor clearColor]];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_WHITE];
    [self dataInit];
}
- (void)conpousNotChooses{
    if (_couponsNum.isNotEmpty) {
        self.conpousLabel.text = _couponsNum;
    }
}
- (void)dataInit{
    _isUseConpous = NO;
    _conpousID = @"";
    
    [[ARRequestModel sharedInstance] user_getRepayInfoWithHopeId:self.hopeID andCB:^(id output) {
        _currentDict = output;
        NSString *amountStr = [NSString stringWithFormat:@"%@",[output objectForKey:@"repayamount"]];
        NSArray *amountArr = [amountStr componentsSeparatedByString:@"."];
        if (amountArr.count>1) {
#pragma mark------------weipeng
            
            self.moneyLabel.text = [NSString stringWithFormat:@"%d",[[amountArr firstObject] intValue]];
            if ([[amountArr lastObject] length]>1) {
                self.littleMoneyLabel.text = [NSString stringWithFormat:@".%@元",[[amountArr lastObject] substringToIndex:2]];
            }
            else
            {
                self.littleMoneyLabel.text = [NSString stringWithFormat:@".%@元",[amountArr lastObject]];
            }
            
#pragma mark------------
        }else{
            self.moneyLabel.text = [NSString stringWithFormat:@"%@",[amountArr firstObject]];
            self.littleMoneyLabel.text = [NSString stringWithFormat:@".00元"];
        }
        self.priodLabel.text = [NSString stringWithFormat:@"%@期",[output objectForKey:@"remainpriod"]];
        self.shengYuMoneyLabel.text = [NSString stringWithFormat:@"%.2f元",[[output objectForKey:@"remainrepay"] floatValue]];
        self.conpousLabel.text = [NSString stringWithFormat:@"%@张优惠券可用",[output objectForKey:@"coucount"]];
        _couponsNum = [NSString stringWithFormat:@"%@张优惠券可用",[output objectForKey:@"coucount"]];
        self.conpousLabel.textColor = [UIColor lightGrayColor];
        
    }];
}
- (IBAction)conpousBtnClikc:(id)sender {
    
    NSString *amountStr = [NSString stringWithFormat:@"%@",[_currentDict objectForKey:@"repayamount"]];
    NSArray *amountArr = [amountStr componentsSeparatedByString:@"."];
    if (amountArr.count>1) {
        self.moneyLabel.text = [NSString stringWithFormat:@"%@",[amountArr firstObject]];
        self.littleMoneyLabel.text = [NSString stringWithFormat:@".%.2f元",[[amountArr lastObject] floatValue]];
    }else{
        self.moneyLabel.text = [NSString stringWithFormat:@"%@",[amountArr firstObject]];
        self.littleMoneyLabel.text = [NSString stringWithFormat:@".00元"];
    }
    
    RePayConpousVC *repayVC = [[RePayConpousVC alloc] init];
    repayVC.amount = [_currentDict objectForKey:@"amount"];
    repayVC.days = [_currentDict objectForKey:@"days"];
    repayVC.priod = [_currentDict objectForKey:@"priod"];
    repayVC.interest = @"";
    repayVC.balance = @"";
    repayVC.faceup = @"";
    repayVC.isface = @"";
    repayVC.face = @"";
    repayVC.delegate = self;
    [repayVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:repayVC animated:YES];
}
- (void)conpousId:(NSString *)conpousID andJSCouponslimit:(NSString *)couponslimit andRuleJS:(NSString *)ruleJS andConpousType:(NSString *)conpousType{
    _isUseConpous = YES;
    _conpousID = conpousID;
    JSContext *newcontext = [[JSContext alloc] init];
    [newcontext evaluateScript:ruleJS];
    _currentRuleJS = newcontext[@"rule"];
    _conpousType = conpousType;
    _conpousID = conpousID;
    
    self.conpousLabel.text = [NSString stringWithFormat:@"%ld张优惠券可用",[[NSString stringWithFormat:@"%@",[_currentDict objectForKey:@"coucount"]] integerValue]-1];
    
    NSString *amount = [_currentDict objectForKey:@"amount"];
    NSInteger day = [[_currentDict objectForKey:@"days"] integerValue];
    NSInteger priod = [[_currentDict objectForKey:@"priod"] integerValue];
    JSValue *shouyi = [_currentRuleJS callWithArguments:@[_conpousType, @"",@"",@"",@"",@"",[_currentDict objectForKey:@"priod"],amount,[NSString stringWithFormat:@"%ld",day+priod*30]]];
    self.moneyLabel.text = [NSString stringWithFormat:@"%ld",[self.moneyLabel.text integerValue]-[shouyi toInt32]/100];
}
- (IBAction)repalyBtnClick:(id)sender {
    if (![WXApi isWXAppInstalled]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未安装微信，请安装后重新尝试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return ;
    }
    [[ARRequestModel sharedInstance] user_repayHope4appWithHopeID:self.hopeID andCouponsid:_conpousID andCB:^(id output) {
        if ([[NSString stringWithFormat:@"%@",[output objectForKey:@"repayType"]] integerValue]==1) {
            [AlertDialogHelper showToast:[output objectForKey:@"errormsg"]];
            return ;
        }
        [[NSUserDefaults standardUserDefaults] setObject:@"HUANKUAN" forKey:@"PAY"];
        [[NSUserDefaults standardUserDefaults] synchronize];
       [WeChat sendOrderToWeChatWithPartnerId:[output objectForKey:@"appId"] andPrepayId:[output objectForKey:@"packageStr"] andNonceStr:[output objectForKey:@"nonceSignPay"] andTimeStamp:[output objectForKey:@"timestampSignPay"] andSign:[output objectForKey:@"signatureSignPay"]];
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
